package org.dinner.order.dao.impl;

import java.util.List;

import org.dinner.order.dao.DishDAO;
import org.dinner.order.dao.DishTypeDAO;
import org.dinner.order.model.Dish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class DishDAOImpl extends BaseDAOImpl<Dish> implements DishDAO {
	
	private static final String ENTITY_NAME = "Dish" ;

	@Autowired
	private DishTypeDAO dishTypeDAO;

	public List<Dish> listByType(int typeId) {
		return sessionFactory.getCurrentSession().getNamedQuery(ENTITY_NAME + ".listByType")
				.setParameter("typeId", typeId).list();
	}

	public Dish getByName(String name) {
		List list = sessionFactory.getCurrentSession().getNamedQuery(ENTITY_NAME + ".listByName")
				.setParameter("name", name).list();
		if (list != null && list.size() > 0) {
			return (Dish) list.get(0);
		} else {
			return null;
		}

	}
	
	@Override
	protected String getQueryPrefix() {
		return ENTITY_NAME;
	}

	public Dish createDish(String name, int typeId, double price,
			boolean isComplex) {
		Dish dish = new Dish();

		dish.setName(name);
		dish.setType(dishTypeDAO.getById(typeId));
		dish.setPrice(price);
		dish.setComplex(isComplex);

		add(dish);

		return dish;
	}

}
